package com.company.goodfuture2020;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] rawData = br.readLine().split(" ");
        int size = rawData.length - 1;
        int[] data = new int[size];
        for (int i = 0; i < size; i++) {
            data[i] = Integer.parseInt(rawData[i]);
        }
        int k = Integer.parseInt(rawData[rawData.length - 1]);
        initMinHeap(data);
        System.out.println(Arrays.toString(data));
        for (int i = 1; i <= k; i++) {
            adjustHeap(data,i, data.length);
        }
        System.out.println(Arrays.toString(data));
        br.close();

    }

    private static void initMinHeap(int[] data){
        for (int i = (int)Math.floor(data.length >> 1) - 1;i >= 0;i--){
            adjustHeap(data,i,data.length);
        }
    }

    private static void adjustHeap(int[] data, int i, int length) {
        int left = i * 2 + 1;
        int right = i * 2 + 2;
        int currentIndex = i;
        if(left < length && data[currentIndex] > data[left]){
            currentIndex = left;
        }
        if(right < length && data[currentIndex] > data[right]){
            currentIndex = right;
        }
        if(i != currentIndex){
            swap(data,i,currentIndex);
            adjustHeap(data,currentIndex,length);
        }

    }

    private static void swap(int[] data, int i, int currentIndex) {
        int temp = data[i];
        data[i] = data[currentIndex];
        data[currentIndex] = temp;
    }

}
